/**
 * ==========================================================
 * Copyright@1996-2009 Abacus International Pte. Ltd.
 * ==========================================================
 */
package com.enzo.prms.common.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @Class name:	GzipUtil.java
 * @author: yiqun
 * @modified: August 22,2012
 */
public class GzipUtil {
    /**
     * Compress the origin byte array and return the result.
     *
     * @param oriBytes
     * @return resultArr
     * @throws java.io.IOException
     */
    public static byte[] compress(byte[] oriBytes) throws IOException {
        ByteArrayOutputStream byteArrOut = new ByteArrayOutputStream();
        GZIPOutputStream GZIPOut = new GZIPOutputStream(byteArrOut);
        GZIPOut.write(oriBytes);

        if (null != GZIPOut) {
            GZIPOut.close();
        }
        return byteArrOut.toByteArray();
    }

    /**
     * Decompress the destination byte array and return the result.
     *
     * @param desBytes
     * @return resultArr
     * @throws IOException
     */
    public static byte[] decompress(byte[] desBytes) throws IOException {
        ByteArrayOutputStream byteArrOut = new ByteArrayOutputStream();
        GZIPInputStream GZIPInput = new GZIPInputStream(new ByteArrayInputStream(desBytes));
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = GZIPInput.read(buffer)) != -1) {
            byteArrOut.write(buffer, 0, len);
        }

        if (null != GZIPInput) {
            GZIPInput.close();
        }
        return byteArrOut.toByteArray();
    }
}
